from fastapi import APIRouter, Request
from fastapi.templating import Jinja2Templates
from ..bit_api import listBrowser, openBrowser

router = APIRouter(prefix="/browsers")
templates = Jinja2Templates(directory="app/templates")

@router.get("/")
async def list_browsers(request: Request):
    # 获取浏览器列表数据
    browsers = await listBrowser()
    
    return templates.TemplateResponse(
        "browser/list.html",
        {
            "request": request,
            "browsers": browsers,
            "title": "浏览器管理"
        }
    )

@router.get("/api/open/{browser_id}")
async def open_browser(browser_id: str):
    try:
        result = await openBrowser(browser_id)
        return {"success": True, "message": "浏览器已打开", "data": result}
    except Exception as e:
        return {"success": False, "message": str(e)}
 